//
//  ReaderBook.m
//  ebook
//
//  Created by 吴超飞 on 16/2/25.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "ReaderBook.h"

@implementation ReaderBook

+(instancetype)shareInstance{
    
    static ReaderBook *model = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        model = [[ReaderBook alloc] init];
    });
    
    return model;
}

-(instancetype)init{
    
    self = [super init];
    if(self){
        self.chapterList = [[NSMutableArray alloc]init];
        self.currentChaptercontent = [[ChapterContent alloc]init];
        self.nextChaptercontent = [[ChapterContent alloc]init];
        self.preChaptercontent = [[ChapterContent alloc]init];
        self.chapter = [[Chapter alloc] init];
    }
    return  self;
}

-(void)setChapterList:(NSMutableArray *)chapterList complete:(void(^)(BOOL exist))complete{
    
    [chapterList sortUsingComparator:^NSComparisonResult(Chapter *obj1, Chapter *obj2) {
        return [obj1.chapterorder intValue] > [obj2.chapterorder intValue];
    }];
    _chapterList = chapterList;
    
    __block BOOL isExistChapter = NO;
    [_chapterList enumerateObjectsUsingBlock:^(Chapter *chapter, NSUInteger idx, BOOL * _Nonnull stop) {
        if([chapter.chapterid integerValue]==[_chapterId integerValue]){
            _chapter = chapter;
            isExistChapter = YES;
            complete(isExistChapter);
            *stop = YES;
        }else if(idx==_chapterList.count-1){
            complete(isExistChapter);
        }
    }];
    
    //chapterid在章节列表中不存在，则将第一章设置为当前章
    if(!isExistChapter)_chapter = _chapterList.firstObject;
}

-(void)setBookmarkList:(NSMutableArray *)bookmarkList{
    
    [bookmarkList sortUsingComparator:^NSComparisonResult(Bookmark *obj1, Bookmark *obj2) {
        return [obj1.chapterid intValue]+[obj1.percent floatValue]>[obj2.chapterid intValue]+[obj2.percent floatValue];
    }];
    _bookmarkList = bookmarkList;
}

-(void)setChapterId:(NSString *)chapterId{
   
    _chapterId = chapterId;
    [_chapterList enumerateObjectsUsingBlock:^(Chapter *chapter, NSUInteger idx, BOOL * _Nonnull stop) {
        if([chapter.chapterid integerValue]==[_chapterId integerValue]){
            _chapter = chapter;
            *stop = YES;
        }
    }];
}

//当前章节的前一章
-(Chapter *)preChapter{
    
    return [self preChapterOfIndex:self.currentChapterIndex];
}

//当前章节的后一章
-(Chapter *)nextChapter{
    
    return [self nextChapterOfIndex:self.currentChapterIndex];
}

//根据index获取其前一章
-(Chapter *)preChapterOfIndex:(NSInteger)charpterIndex{
    
    return (charpterIndex>0&&charpterIndex<self.chapterList.count)?[_chapterList objectAtIndex:charpterIndex-1]:nil;
}

//根据index获取其后一章
-(Chapter *)nextChapterOfIndex:(NSInteger)charpterIndex{
    
    return ((_chapterList.count)>charpterIndex+1&&charpterIndex<self.chapterList.count)?[_chapterList objectAtIndex:charpterIndex+1]:nil;
}

//当前章节序号
-(NSInteger)currentChapterIndex{
    
    return [self.chapterList indexOfObject:_chapter];
}

//判断是否第一章或最后一章
-(BOOL)isFirstOrLastChapter{
    return self.currentChapterIndex==0||self.currentChapterIndex==self.chapterList.count-1;
}

//获取在章节列表中的对象
-(Chapter *)listChapter:(Chapter *)chapter{
    
    for(Chapter *obj in self.chapterList) {
        if([obj.chapterid intValue]==[chapter.chapterid intValue])
            return obj;
    }
    return nil;
}

//获取指定章节的前一章
-(Chapter *)chapterBefore:(Chapter *)chapter{
    
    Chapter *pre = nil;
    if([chapter.chapterid integerValue]==[_chapterId integerValue]){
        pre = self.preChapter;
    }else{
        Chapter *chapterInlist = [self listChapter:chapter];
        pre = [self preChapterOfIndex:[self.chapterList indexOfObject:chapterInlist]];
    }
    
    return pre;
}

//获取指定章节的后一章
-(Chapter *)chapterAfter:(Chapter *)chapter{
    
    Chapter *next = nil;
    if([chapter.chapterid integerValue]==[_chapterId integerValue]){
        next = self.nextChapter;
    }else{
        Chapter *chapterInlist = [self listChapter:chapter];
        next = [self nextChapterOfIndex:[self.chapterList indexOfObject:chapterInlist]];
    }
    
    return next;
}

-(void)reset{
    
    self.currentChaptercontent = nil;
    self.preChaptercontent = nil;
    self.nextChaptercontent = nil;
    [self.bookmarkList removeAllObjects];
    [self.chapterList removeAllObjects];
}

@end
